
/**
 * 
 */

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 文件转换工具类
 * 
 * @author yuqinyi
 *
 */
public class PX2SIZEUtils {

    private static double s2d(String text) {
        return s2d(text, -1D);
    }

    private static double s2d(String text, double defaultValue) {
        try {
            return Double.parseDouble(Pattern.compile("[^0-9]").matcher(text).replaceAll("").trim());
        } catch (Exception e) {
            return defaultValue;
        }
    }

    private static boolean write(String cont, File dist) {
        try {
            BufferedWriter writer = new BufferedWriter(new FileWriter(dist));
            writer.write(cont);
            writer.flush();
            writer.close();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    private static String replace(String src) {
        String pattern = "(\\d+)px";

//        String src = "                    padding: 0 55px;";
//        System.out.println("Input:" + src);

        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(src);
//        double i = 0D;
//        if (m.find()) {
//            i = s2d(m.group(0));
//            System.out.println(i);
//        }
//        String size = null;
//        if (i == 0D) {
//            size = "0";
//        } else {
//            size = "size(" + (i * 480 / 375) + ")";
//        }
//        String dist = m.replaceAll(size);
        StringBuffer sb = new StringBuffer();
        while (m.find()) {
            String key = m.group(1);
            String size = null;
            double i = Double.parseDouble(key);
            if (i == 0D) {
                size = "0";
            } else {
                size = "size(" + (i * 480 / 375) + ")";
            }
            m.appendReplacement(sb, size == null ? "" : size);
        }
        m.appendTail(sb);
        String dist = sb.toString();
//        System.out.println("ReplaceAll:" + dist);
        m.reset();

        return dist;
    }

    private static String readAndReplace(String path) {
        StringBuffer res = new StringBuffer();
        String line = null;
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(path), "UTF-8"));
            boolean doConvert = false;
            while ((line = reader.readLine()) != null) {
                if (doConvert) {
                    String line2 = replace(line);
                    res.append(line2 + "\n");
                } else {
                    doConvert = line.indexOf("<style") > -1;
                    res.append(line + "\n");
                }
            }
            reader.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return res.toString();
    }

    public static void convert(String path) throws IOException {
        String fn = "input/" + path;

        File dist = new File("output/" + path);
        File dp = dist.getParentFile();
        if (!dp.exists()) {
            dp.mkdirs();
        }

        if (!dist.exists()) {
            dist.createNewFile();
        }

        System.out.println("处理后文件路径：" + dist.getCanonicalPath());
        String dc = readAndReplace(fn);
        write(dc, dist);
    }

    public static void main(String[] args) {
        try {
            if (args == null || args.length == 0 || args[0] == null) {
                System.out.println("需要输入文件名: PX2SIZE480 XXX.vue");
                return;
            }
            convert(args[0]);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
